#!/bin/sh

#
# Test streaming a firmware update through fwup and make sure
# that fwup exits as soon as it's done without trying to read
# all input. This makes things much faster especially when there
# are errors, so we want to make sure that it works.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

if [ -z $TIMEOUT ]; then
    echo "GNU coreutils' timeout command unavailable so skipping test."
    exit 77
fi

cat >$CONFIG <<EOF
file-resource TEST {
	host-path = "${TESTFILE_1K}"
}
task complete {
	on-resource TEST { raw_write(1) }
}
EOF

# Create the firmware file like normal
$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Pipe the contents of the firmware file through fwup
# Without the fix, this would go on forever if not for
# the timeout.
cat $FWFILE /dev/zero | $TIMEOUT 10s $FWUP_APPLY -a -d $IMGFILE -i - -t complete

# Check that the test file ends up in the right spot.
cmp_bytes 1024 $TESTFILE_1K $IMGFILE 0 512

